<?php
namespace app\admin\controller;

use think\Db;

class User extends Common
{
    public function _initialize()
    {
        parent::_initialize();
    }
    public function index()
    {
        if (request()->isPost()) {
            $add_time = input('post.user_addtime');
            $gender = input('post.sex');
            $nickName = input('post.realname');
            $level = input('post.level');
            $where = array();
            if ($gender != '') {
                $where['sex'] = $gender;
            }

            if ($add_time) {
                $where['user_addtime'] = timeData($add_time);
            }

            if ($nickName) {
                $where['realname'] = ['like', '%' . $nickName . '%'];
            }
            if($level){
                $where['c.level'] = $level;
            }

            $page = input('page') ? input('page') : 1;
            $pageSize = input('limit') ? input('limit') : config('common.page_sizes');
            $list = db('user')
                ->alias('u')
                ->join('vip_cate c','u.vip_id = c.level')
                ->where($where)
                ->order('user_id desc')
                ->field('u.user_id as user_id,u.realname as realname,u.nickname as nickname,u.sex as sex, u.money as money,u.consumption_money as consumption_money,u.real_money as real_money,u.openid as openid,u.avatar as avatar,u.father_id as father_id,u.phone as phone,u.vip_id as vip_id,u.vip_number as vip_number,u.user_status as user_status,u.user_addtime as user_addtime,u.eqimg as eqimg,u.idcard as idcard')
                ->paginate(array('list_rows' => $pageSize, 'page' => $page))
                ->toArray();

            $list = model('user')->userListData($list);
            foreach ($list['data'] as $key => &$val) {
                if ($val['father_id'] != 0) {
                    $val['father'] = db('user')->where(['user_id' => $val['father_id']])->value('realname');
                    if (!$val['father']) {
                        $val['father'] = db('user')->where(['user_id' => $val['father_id']])->value('nickname');
                    }
                }
            }

            return $result = ['code' => 0, 'msg' => config('common.data_success'), 'data' => $list['data'], 'count' => $list['total'], 'rel' => 1];
        }
        return $this->fetch('', ['statuslist' => model('user')->genderData(),'list_category'=>db('vip_cate')->select()]);
    }
    public function adminstate()
    {
        $data = input('post.');

        $site = Db::table('think_user')->update($data);

        // if ($site !== false) return $site;
        if ($site) {
            $res = ['errorCode' => 0, 'msg' => '会员状态提交成功'];
        } else {
            $res = ['errorCode' => 800, 'msg' => '会员状态提交失败'];
        }

        return $res;
    }
    public function edit()
    {
        $id = input('id');
 
        $list = db('user')->where(['user_id' => $id])->find();
            $list['vip_id'] = db('vip_cate')->where(['level' => $list['vip_id']])->field('title')->find()['title'];
        if (isset($list['father_id'])) {
            $list['father_id'] = db('user')->where(['user_id' => $list['father_id']])->field('realname')->find()['realname'];
        }
        
        return $this->fetch('',['list' => $list]);
    }

}
